package Entities 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import screens.InstrumentsTab;
	import screens.NoteCanvas;
	import screens.InstrumentScreen;
	/**
	 * ...
	 * @author Andy Wolff
	 */
	public class InstrumentMC extends MovieClip
	{
		[Embed(source = '../images/Buttons/eye_closed.png')] private var eye_closedIMG:Class;
		[Embed(source = '../images/Buttons/eye_open.png')] private var eye_openIMG:Class;
		
		private var canvas:NoteCanvas;
		private var tab:InstrumentsTab;
		public var instrument:int=0;
		private var selected:Boolean;
		public function select():void { 
				var a:Array = tab.instrumentMCs;
				canvas.getInstrument(instrument).visible = true;
				canvas.selectInstrument(canvas.getInstrument(instrument));
				var newme:InstrumentMC = new InstrumentMC(canvas, tab, instrument);
				a.splice(a.indexOf(this, 0), 1, newme);
				for (var i:int = 0; i < a.length; i++) { 
					var il:InstrumentMC = (a[i] as InstrumentMC); 
					if (il!=null && il!=this) il.deselect();
				}
				kill();
				tab.refresh();
			}
		public function deselect():void {
				if (!selected) return;
				var a:Array = tab.instrumentMCs;
				selected = false;
				var newme:InstrumentMC = new InstrumentMC(canvas, tab, instrument);
				a.splice(a.indexOf(this, 0), 1, newme);
				kill();
			}
		public function hide():void {
				if (!canvas.getInstrument(instrument).visible || selected) return;
				var a:Array = tab.instrumentMCs;
				canvas.getInstrument(instrument).visible = false;
				var newme:InstrumentMC = new InstrumentMC(canvas, tab, instrument);
				a.splice(a.indexOf(this, 0), 1, newme);
				kill();
				tab.refresh();
			}
		public function show():void {
			if (canvas.getInstrument(instrument).visible) return;
			var a:Array = tab.instrumentMCs;
			canvas.getInstrument(instrument).visible = true;
			var newme:InstrumentMC = new InstrumentMC(canvas, tab, instrument);
			a.splice(a.indexOf(this, 0), 1, newme);
			kill();
			tab.refresh();
		}
		public function InstrumentMC(canvas:NoteCanvas,tab:InstrumentsTab,instrument:int) 
		{
			this.instrument = instrument;
			this.tab = tab;
			this.canvas = canvas;
			selected = canvas.getInstrument(instrument) == canvas.getCurrentInstrument();
			graphics.beginFill(canvas.getInstrument(instrument).color, 0.5);
			graphics.lineStyle(2, selected?0xFFFFFF:0x444444, 0.5);
			graphics.drawRect(-14, -2, 114, 28);
			graphics.endFill();
			var type:int = canvas.getInstrument(instrument).type;
			if (type < 1) addChild(new InstrumentScreen.fluteimg());
			else if (type < 2) addChild(new InstrumentScreen.oboeimg());
			else addChild(new InstrumentScreen.trumpetimg());
			var eye:MovieClip = new MovieClip();
			eye.addChild((canvas.getInstrument(instrument).visible)?new eye_openIMG():new eye_closedIMG());
			eye.x = 110;
			addChild(eye);
			addEventListener(MouseEvent.CLICK, click);
		}
		
		public function click(me:MouseEvent):void {
			trace(me.stageX);
			if (me.stageX<700) select();
			else { 
				if (canvas.getInstrument(instrument).visible) hide();
				else show();
			}
		}
		
		public function kill():void {
			removeEventListener(MouseEvent.CLICK, click);
			//tab.removeChild(this);
		}
	}

}